package com.sls.api.game.parser;

import java.io.File;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.sls.api.game.pojo.XingOrderStatusInfo;

/**
 * 
 * @description 解析星启天配置文件
 * @author Wangxuegang
 * @date 2012-12-28
 */
public class ReadXingConfig {

	private static Logger log = Logger.getLogger(ReadXingConfig.class);

	/**
	 * 
	 * @return
	 */
	public static XingOrderStatusInfo readConfig() {
		InputStream xmlStream = Parser.class.getClassLoader()
				.getResourceAsStream(
						"conf" + File.separator + "XingQiTianServer.xml");

		XingOrderStatusInfo info = new XingOrderStatusInfo();
		try {
			// 得到DOM解析器的工厂实例
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			// 从DOM工厂中获得DOM解析器
			DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();

			// 把要解析的xml文档读入DOM解析器
			Document doc = dbBuilder.parse(xmlStream);
			Element root = doc.getDocumentElement();
			NodeList childNodes = root.getChildNodes(); // 获得根元素下的子节点
			// 遍历子节点
			for (int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i); // 对每个子节点进行判断
				if ("userKey".equals(node.getNodeName())) { // 第二层节点
					String userKey = node.getTextContent();
					info.setUserKey(userKey);
				}
			}

			return info;
		} catch (Exception e) {
			log.error(e);
		}

		return null;
	}

}
